home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivmulti.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  60.7 KB  |  2,323 lines

  1. unit IvMulti;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8. {$IFDEF WIN32}
  9.   Windows,
  10. {$ELSE}
  11.   WinTypes, WinProcs,
  12. {$ENDIF}
  13. {$IFDEF _DEBUG}
  14.   DebugFil,
  15. {$ENDIF}
  16.   SysUtils, Classes, Dialogs, Forms, Controls, Graphics, TypInfo,
  17.   IvDictio, IvCommon;
  18.  
  19. const
  20.   TARGETS_C = 'Targets.txt';
  21.  
  22.   TARGETS_VERSION_C = 1;
  23.  
  24. type
  25.   TIvTargetType = (ivttInclude, ivttExclude);
  26.  
  27.   TIvTargetProperty = class(TObject)
  28.   protected
  29.     FTargetClassName: String;
  30.     FTargetPropertyName: String;
  31.     FTargetType: TIvTargetType;
  32.  
  33.   public
  34.     constructor Create(
  35.       const targetClassName, targetPropertyName: String;
  36.       targetType: TIvTargetType);
  37.     constructor CreateDefault;
  38.  
  39.     procedure Assign(target: TIvTargetProperty);
  40.     function IsEqual(target: TIvTargetProperty): Boolean;
  41.     function Copy: TIvTargetProperty;
  42.  
  43.     function IsDefault: Boolean;
  44.     procedure SetDefault;
  45.  
  46.     property TargetClassName: String read FTargetClassName write FTargetClassName;
  47.     property TargetPropertyName: String read FTargetPropertyName write FTargetPropertyName;
  48.     property TargetType: TIvTargetType read FTargetType write FTargetType;
  49.   end;
  50.  
  51.   TIvTargetProperties = class(TPersistent)
  52.   protected
  53.     FExcludeCount: Integer;
  54.     FItems: TList;
  55.  
  56.     function GetCount: Integer;
  57.     function GetItems(index: Integer): TIvTargetProperty;
  58.  
  59.     procedure ReadData(reader: TReader);
  60.  
  61.     procedure DefineProperties(Filer: TFiler); override;
  62.  
  63.     function DoesMatch(target: TIvTargetProperty; const obj, name: String): Boolean;
  64.  
  65.   public
  66.     constructor Create;
  67.     destructor Destroy; override;
  68.  
  69.     procedure Assign(source: TPersistent); override;
  70.     function IsEqual(targets: TIvTargetProperties): Boolean;
  71.     function Copy: TIvTargetProperties;
  72.  
  73.     function IsDefault: Boolean;
  74.     procedure SetDefault;
  75.  
  76.     function Find(item: TIvTargetProperty): Integer;
  77.  
  78.     function Add(item: TIvTargetProperty): Integer;
  79.     procedure Delete(index: integer);
  80.  
  81.     function IsObjectInTargets(const obj: String): Boolean;
  82.     function IsComponentInTargets(const component: TComponent): Boolean;
  83.     function IsPropertyInTargets(const obj, name: String): Boolean;
  84.     function IsPropertyExcluded(const obj, name: String): Boolean;
  85.  
  86.     property Count: Integer read GetCount;
  87.     property ExcludeCount: Integer read FExcludeCount write FExcludeCount;
  88.     property Items[index: Integer]: TIvTargetProperty read GetItems; default;
  89.   end;
  90.  
  91.   { TIvTranslator }
  92.  
  93.   TIvTranslator = class;
  94.  
  95.   TIvTranslateNotifyEvent = procedure(translator: TIvTranslator; component: TComponent) of object;
  96.  
  97.   TIvRestriction = (ivrBuildInTranslation, ivrCustomTranslation, ivrFlip,
  98.     ivrReadingOrder, ivrThisComponentOnly, ivrCharset);
  99.   TIvRestrictions = set of TIvRestriction;
  100.  
  101.   TIvRestrictComponentEvent = procedure(
  102.     translator: TIvTranslator;
  103.     component: TComponent;
  104.     var restrictions: TIvRestrictions) of object;
  105.   TIvRestrictObjectEvent = procedure(
  106.     translator: TIvTranslator;
  107.     obj: TObject;
  108.     var translate: Boolean) of object;
  109.   TIvRestrictPropertyEvent = procedure(
  110.     translator: TIvTranslator;
  111.     obj: TObject;
  112.     const name: String;
  113.     var translate: Boolean) of object;
  114.  
  115.   TIvTranslatorOption = (ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte,
  116.     ivtoMirrorBiDirectional, ivtoChangeFontCharset, ivtoUpdateLocaleProperty,
  117.     ivtoAutoOpen, ivtoTranslateSystemMenu);
  118.   TIvTranslatorOptions = set of TIvTranslatorOption;
  119.  
  120.   TIvReadingOrder = (ivroLeftToRight, ivroRightToLeft);
  121.  
  122.   TIvChangeReadingOrderEvent = procedure(
  123.     translator: TIvTranslator;
  124.     component: TComponent;
  125.     readingOrder: TIvReadingOrder) of object;
  126.  
  127.   TIvBidirectionalState = class(TObject)
  128.   public
  129. {$IFDEF IVVB}
  130.     DefaultFontName: String;
  131.     DefaultFontSize: longint;
  132. {$ENDIF}
  133.     Control: TControl;
  134.     Flipped: Boolean;
  135.     OriginalLeft: Integer;
  136.     ReadingOrder: TIvReadingOrder;
  137.  
  138.     constructor Create(const control: TControl);
  139.   end;
  140.  
  141.   TIvTranslatorTableItem = class(TObject)
  142.   public
  143.     Obj: TObject;
  144.     Component: String;
  145.     PropertyName: String;
  146.     Native: String;
  147.     Current: String;
  148.  
  149.     constructor Create(
  150.       obj: TObject;
  151.       const component, propertyName, native, current: String);
  152.   end;
  153.  
  154.   TIvTranslatorTable = class(TObject)
  155.   protected
  156.     FRows: TList;
  157.  
  158.     function GetCount: Integer;
  159.     function GetItem(row: Integer): TIvTranslatorTableItem;
  160.  
  161.   public
  162.     constructor Create;
  163.     destructor Destroy; override;
  164.  
  165.     procedure Add(item: TIvTranslatorTableItem);
  166.     procedure Clear;
  167.  
  168.     property Count: Integer read GetCount;
  169.     property Items[row: Integer]: TIvTranslatorTableItem read GetItem;
  170.   end;
  171.  
  172.   TIvModule = class(TComponent)
  173.   public
  174.     function TranslateComponent(
  175.       translator: TIvTranslator;
  176.       component: TComponent): Boolean; virtual;
  177.  
  178.     function FlipControl(
  179.       translator: TIvTranslator;
  180.       control: TControl;
  181.       state: TIvBidirectionalState): Boolean; virtual;
  182.  
  183.     function UnFlipControl(
  184.       translator: TIvTranslator;
  185.       control: TControl;
  186.       state: TIvBidirectionalState): Boolean; virtual;
  187.  
  188.     function ChangeComponentReadingOrder(
  189.       translator: TIvTranslator;
  190.       component: TComponent): Boolean; virtual;
  191.   end;
  192.  
  193.   TIvModules = class(TObject)
  194.   private
  195.     FItems: TList;
  196.  
  197.     function GetCount: Integer;
  198.     function GetItems(index: Integer): TIvModule;
  199.  
  200.   public
  201.     constructor Create;
  202.     destructor Destroy; override;
  203.  
  204.     procedure Add(item: TIvModule);
  205.  
  206.     function TranslateComponent(
  207.       translator: TIvTranslator;
  208.       component: TComponent): Boolean;
  209.  
  210.  
  211.     property Count: Integer read GetCount;
  212.     property Items[index: Integer]: TIvModule read GetItems; default;
  213.   end;
  214.  
  215.   TIvTranslator = class(TIvCustomTranslator)
  216.   protected
  217.     FNativeLayout: TIvReadingOrder;
  218.     FReadingOrder: TIvReadingOrder;
  219.     FHost: TComponent;
  220.     FTranslatorTable: TIvTranslatorTable;
  221.     FTargets: TIvTargetProperties;
  222.     FOptions: TIvTranslatorOptions;
  223.     FRestrictions: TIvRestrictions;
  224.     FDefaultFontName: String;
  225.     FDefaultFontSize: Integer;
  226.     FMultiByteScale: Integer;
  227.     FBidirectionalStates: TList;
  228.     FOnTranslate: TIvTranslateNotifyEvent;
  229.     FOnScale: TIvTranslateNotifyEvent;
  230.     FOnRestrictComponent: TIvRestrictComponentEvent;
  231.     FOnRestrictObject: TIvRestrictObjectEvent;
  232.     FOnRestrictProperty: TIvRestrictPropertyEvent;
  233.  
  234.     function GetAutoOpen: Boolean;
  235.     procedure SetAutoOpen(value: Boolean);
  236.  
  237.     function GetHost: TComponent; virtual;
  238.  
  239.     procedure SetTargets(value: TIvTargetProperties);
  240.  
  241.     procedure Notification(component: TComponent; operation: TOperation); override;
  242.  
  243.     procedure TranslateHost; override;
  244.     procedure TranslateObject(obj: TObject; restrictions: TIvRestrictions); virtual;
  245.     procedure TranslateSubComponent(
  246.       component: TComponent;
  247.       parentRestrictions: TIvRestrictions); virtual;
  248.  
  249.     procedure CheckHostFont; virtual;
  250.  
  251.     function GetBidirectionalState(control: TControl; allocNew: Boolean): TIvBidirectionalState;
  252.  
  253.     procedure FlipControl(control: TControl; state: TIvBidirectionalState); virtual;
  254.     procedure UnFlipControl(control: TControl; state: TIvBidirectionalState); virtual;
  255.  
  256.     procedure LanguageChanged(languageChanged, localeChanged: Boolean); override;
  257.  
  258.     procedure ReadTargets(reader: TReader);
  259.     procedure WriteTargets(writer: TWriter);
  260. {
  261.     procedure AddObjectTargets(
  262.       obj: TObject;
  263.       targets: TIvTargetProperties;
  264.       prompt: Boolean);
  265. }
  266.     procedure DefineProperties(Filer: TFiler); override;
  267.  
  268.     procedure Loaded; override;
  269.  
  270. {$IFDEF WIN32}
  271.     function GetSystemMenuWinHandle: THandle; override;
  272. {$ENDIF}
  273.  
  274.     { Override these in your derived translators }
  275.  
  276.     procedure TranslateComponent(component: TComponent); virtual;
  277.     procedure ChangeComponentReadingOrder(component: TComponent); virtual;
  278.  
  279.   public
  280.     constructor Create(owner: TComponent); override;
  281.     destructor Destroy; override;
  282.  
  283.     procedure Translate; override;
  284.     procedure Unbind; override;
  285.     procedure UnbindAndSetNative; override;
  286.  
  287.     procedure AddObjectTargets(
  288.       obj: TObject;
  289.       targets: TIvTargetProperties;
  290.       prompt: Boolean);
  291.  
  292.     function DoTranslateString(
  293.       obj: TObject;
  294.       const propertyName, value: String): String;
  295.     function DoTranslateContextString(
  296.       obj: TObject;
  297.       const component, propertyName, value: String): String;
  298.  
  299.     procedure DoTranslateStrings(
  300.       obj: TObject;
  301.       const component, propertyName: String; value: TStrings);
  302.  
  303.     procedure AddTranslation(
  304.       obj: TObject;
  305.       const component, propertyName, value: String);
  306.  
  307.     procedure Open(host: TComponent); virtual;
  308.     procedure Close; virtual;
  309.  
  310.     procedure DetectTargets(
  311.       targets: TIvTargetProperties;
  312.       prompt: Boolean);
  313.  
  314.     function GetNativePropertyValue(
  315.       component: TObject;
  316.       const propertyName: String): String;
  317.     function GetNativePropertyValueEx(
  318.       component: TObject;
  319.       const propertyName, currentValue: String): String;
  320.  
  321.     procedure UpdateControl(control: TControl); virtual;
  322.     procedure UpdateControls; virtual;
  323.  
  324.     property Host: TComponent read GetHost;
  325.     property ReadingOrder: TIvReadingOrder read FReadingOrder;
  326.     property TranslatorTable: TIvTranslatorTable read FTranslatorTable;
  327.  
  328.   published
  329.     property NativeLayout: TIvReadingOrder read FNativeLayout write FNativeLayout default ivroLeftToRight;
  330.     property Targets: TIvTargetProperties read FTargets write SetTargets;
  331.     property Options: TIvTranslatorOptions read FOptions write FOptions default
  332.       [ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte, ivtoMirrorBiDirectional,
  333.        ivtoChangeFontCharset, ivtoAutoOpen, ivtoTranslateSystemMenu];
  334.     property MultiByteScale: Integer read FMultiByteScale write FMultiByteScale default 100;
  335.     property OnTranslate: TIvTranslateNotifyEvent read FOnTranslate write FOnTranslate;
  336.     property OnScale: TIvTranslateNotifyEvent read FOnScale write FOnScale;
  337.     property OnRestrictComponent: TIvRestrictComponentEvent read FOnRestrictComponent write FOnRestrictComponent;
  338.     property OnRestrictObject: TIvRestrictObjectEvent read FOnRestrictObject write FOnRestrictObject;
  339.     property OnRestrictProperty: TIvRestrictPropertyEvent read FOnRestrictProperty write FOnRestrictProperty;
  340.   end;
  341.  
  342. procedure IvCenterControl(parent, control: TControl);
  343.  
  344. var
  345.   Modules: TIvModules;
  346.  
  347. implementation
  348.  
  349. uses
  350. {$IFDEF IVVB}
  351.   CompOCX,
  352. {$ENDIF}
  353. {$IFDEF IVWIDE}
  354.   Checklst,
  355. {$ENDIF}
  356.   Messages, StdCtrls, Menus,
  357.   IvParser;
  358.  
  359. const
  360.   NO_DICTIONARY_C = 'No dictionary has been assigned to the translator component (%s on "%s" form)';
  361.   NO_TARGETS_C = 'No target properties has been assigned to the translator component (%s on "%s" form)';
  362.   INVALID_HOST_C = 'The host component (owner) of the translator must be a TControl (%s on "%s" form)';
  363.  
  364. { TIvTargetProperty }
  365.  
  366. constructor TIvTargetProperty.Create(
  367.   const targetClassName, targetPropertyName: String;
  368.   targetType: TIvTargetType);
  369. begin
  370.   inherited Create;
  371.   FTargetClassName := targetClassName;
  372.   FTargetPropertyName := targetPropertyName;
  373.   FTargetType := targetType;
  374. end;
  375.  
  376. constructor TIvTargetProperty.CreateDefault;
  377. begin
  378.   Create('', '', ivttInclude);
  379. end;
  380.  
  381. function TIvTargetProperty.Copy: TIvTargetProperty;
  382. begin
  383.   Result := TIvTargetProperty.Create(FTargetClassName, FTargetPropertyName, FTargetType);
  384. end;
  385.  
  386. procedure TIvTargetProperty.Assign(target: TIvTargetProperty);
  387. begin
  388.   FTargetClassName := target.FTargetClassName;
  389.   FTargetPropertyName := target.FTargetPropertyName;
  390.   FTargetType := target.FTargetType;
  391. end;
  392.  
  393. function TIvTargetProperty.IsEqual(target: TIvTargetProperty): Boolean;
  394. begin
  395.   Result :=
  396.     (FTargetClassName = target.FTargetClassName) and
  397.     (FTargetPropertyName = target.FTargetPropertyName) and
  398.     (FTargetType = target.FTargetType);
  399. end;
  400.  
  401. function TIvTargetProperty.IsDefault: Boolean;
  402. begin
  403.   Result := (FTargetPropertyName = '') and
  404.     (FTargetClassName = '') and
  405.     (FTargetType = ivttInclude);
  406. end;
  407.  
  408. procedure TIvTargetProperty.SetDefault;
  409. begin
  410.   FTargetClassName := '';
  411.   FTargetPropertyName := '';
  412.   FTargetType := ivttInclude;
  413. end;
  414.  
  415.  
  416. { TIvTargetProperties }
  417.  
  418. constructor TIvTargetProperties.Create;
  419. begin
  420.   inherited Create;
  421.   FExcludeCount := 0;
  422.   FItems := TList.Create;
  423. end;
  424.  
  425. destructor TIvTargetProperties.Destroy;
  426. begin
  427.   SetDefault;
  428.   FItems.Free;
  429.   inherited Destroy;
  430. end;
  431.  
  432. procedure TIvTargetProperties.Assign(source: TPersistent);
  433. var
  434.   i: Integer;
  435. begin
  436.   if source is TIvTargetProperties then
  437.   begin
  438.     SetDefault;
  439.     FExcludeCount := TIvTargetProperties(source).FExcludeCount;
  440.     for i := 0 to TIvTargetProperties(source).Count - 1 do
  441.       Add(TIvTargetProperty(TIvTargetProperties(source).Items[i].Copy));
  442.     Exit;
  443.   end;
  444.   inherited Assign(source);
  445. end;
  446.  
  447. function TIvTargetProperties.IsEqual(targets: TIvTargetProperties): Boolean;
  448. var
  449.   i, j: Integer;
  450.   found: Boolean;
  451. begin
  452.   if Count = targets.Count then
  453.   begin
  454.     for i := 0 to Count - 1 do
  455.     begin
  456.       found := False;
  457.  
  458.       for j := 0 to targets.Count - 1 do
  459.       begin
  460.         if Items[i].IsEqual(targets.Items[j]) then
  461.         begin
  462.           found := True;
  463.           Break;
  464.         end;
  465.       end;
  466.  
  467.       if not found then
  468.       begin
  469.         Result := False;
  470.         Exit;
  471.       end;
  472.     end;
  473.     
  474.     Result := True;
  475.   end
  476.   else
  477.     Result := False;
  478. end;
  479.  
  480. procedure TIvTargetProperties.ReadData(reader: TReader);
  481. var
  482.   i, count: Integer;
  483.   target: TIvTargetProperty;
  484. begin
  485.   SetDefault;
  486.   reader.ReadListBegin;
  487.   count := reader.ReadInteger;
  488.   for i := 0 to count - 1 do
  489.   begin
  490.     target := TIvTargetProperty.CreateDefault;
  491.     target.TargetClassName := reader.ReadString;
  492.     target.TargetPropertyName := reader.ReadString;
  493.     FItems.Add(target);
  494.   end;
  495.   reader.ReadListEnd;
  496. end;
  497.  
  498. procedure TIvTargetProperties.DefineProperties(filer: TFiler);
  499. begin
  500.   inherited DefineProperties(Filer);
  501.   Filer.DefineProperty('Data', ReadData, nil, filer is TReader);
  502. end;
  503.  
  504. function TIvTargetProperties.GetCount: Integer;
  505. begin
  506.   Result := FItems.Count;
  507. end;
  508.  
  509. function TIvTargetProperties.GetItems(index: Integer): TIvTargetProperty;
  510. begin
  511.   Result := FItems[index];
  512. end;
  513.  
  514. function TIvTargetProperties.Find(item: TIvTargetProperty): Integer;
  515. var
  516.   i: Integer;
  517. begin
  518.   for i := 0 to Count - 1 do
  519.     if Items[i].IsEqual(item) then
  520.     begin
  521.       Result := i;
  522.       Exit;
  523.     end;
  524.     
  525.   Result := -1;
  526. end;
  527.  
  528. function TIvTargetProperties.Add(item: TIvTargetProperty): Integer;
  529. begin
  530.   Result := FItems.Add(item);
  531.   if item.TargetType = ivttExclude then
  532.     Inc(FExcludeCount);
  533. end;
  534.  
  535. procedure TIvTargetProperties.Delete(index: integer);
  536. begin
  537.   if Items[index].TargetType = ivttExclude then
  538.     Dec(FExcludeCount);
  539.   FItems.Delete(index);
  540. end;
  541.  
  542. function TIvTargetProperties.Copy: TIvTargetProperties;
  543. begin
  544.   Result := TIvTargetProperties.Create;
  545.   Result.Assign(Self);
  546. end;
  547.  
  548. function TIvTargetProperties.IsDefault: Boolean;
  549. begin
  550.   Result := Count = 0;
  551. end;
  552.  
  553. procedure TIvTargetProperties.SetDefault;
  554. var
  555.   I: Integer;
  556. begin
  557.   FExcludeCount := 0;
  558.   for i := 0 to Count - 1 do
  559.     Items[i].Free;
  560.   FItems.Clear;
  561. end;
  562.  
  563. function TIvTargetProperties.DoesMatch(
  564.   target: TIvTargetProperty;
  565.   const obj, name: String): Boolean;
  566. begin
  567.   { If the target class name is null or matches to the object and the target
  568.     propety name matches to the name the property should be translated.
  569.  
  570.     Similary the property should be translated if a target is a class target.
  571.     This means a target with no property name. In such a case all string
  572.     properties of the class should be translated. }
  573.  
  574.   Result :=
  575.     (((target.TargetClassName = '') or
  576.       (CompareText(target.TargetClassName, obj) = 0)) and
  577.     (CompareText(name, target.TargetPropertyName) = 0)) or
  578.     ((CompareText(target.TargetClassName, obj) = 0) and
  579.      (target.TargetPropertyName = ''));
  580. end;
  581.  
  582. function TIvTargetProperties.IsPropertyExcluded(const obj, name: String): Boolean;
  583. var
  584.   i: Integer;
  585.   target: TIvTargetProperty;
  586. begin
  587.   Result := False;
  588.  
  589.   if FExcludeCount > 0 then
  590.   begin
  591.     for i := 0 to Count - 1 do
  592.     begin
  593.       target := Items[i];
  594.       if (target.TargetType = ivttExclude) and DoesMatch(target, obj, name) then
  595.       begin
  596.         Result := True;
  597.         Break;
  598.       end;
  599.     end;
  600.   end;
  601. end;
  602.  
  603. function TIvTargetProperties.IsPropertyInTargets(const obj, name: String): Boolean;
  604. var
  605.   i: Integer;
  606.   target: TIvTargetProperty;
  607. begin
  608.   Result := False;
  609.  
  610.   { If the targets properties contains exclude targets checks them first }
  611.  
  612.   if IsPropertyExcluded(obj, name) then
  613.     Exit;
  614.  
  615.   { Checks if any include target matches the give property }
  616.  
  617.   for i := 0 to Count - 1 do
  618.   begin
  619.     target := Items[i];
  620.     if (target.TargetType = ivttInclude) and DoesMatch(target, obj, name) then
  621.     begin
  622.       Result := True;
  623.       Exit;
  624.     end;
  625.   end;
  626. end;
  627.  
  628. function TIvTargetProperties.IsObjectInTargets(const obj: String): Boolean;
  629. var
  630.   i: Integer;
  631.   target: TIvTargetProperty;
  632.  
  633.   function DoesMatch: Boolean;
  634.   begin
  635.     Result :=
  636.       (CompareText(target.TargetClassName, obj) = 0) and
  637.       (target.TargetPropertyName = '');
  638.   end;
  639.  
  640. begin
  641.   Result := False;
  642.  
  643.   { If the targets properties contains exclude targets checks them first }
  644.  
  645.   if FExcludeCount > 0 then
  646.   begin
  647.     for i := 0 to Count - 1 do
  648.     begin
  649.       target := Items[i];
  650.       if (target.TargetType = ivttExclude) and DoesMatch then
  651.         Exit;
  652.     end;
  653.   end;
  654.  
  655.   { Checks if any include target matches the give property }
  656.  
  657.   for i := 0 to Count - 1 do
  658.   begin
  659.     target := Items[i];
  660.     if (target.TargetType = ivttInclude) and DoesMatch then
  661.     begin
  662.       Result := True;
  663.       Exit;
  664.     end;
  665.   end;
  666. end;
  667.  
  668. function TIvTargetProperties.IsComponentInTargets(const component: TComponent): Boolean;
  669. var
  670.   i: Integer;
  671.   target: TIvTargetProperty;
  672.  
  673.   function DoesMatch: Boolean;
  674.   begin
  675.     Result :=
  676.       (CompareText(target.TargetClassName, component.ClassName) = 0) and
  677.       (CompareText(target.TargetPropertyName, '') = 0);
  678.   end;
  679.  
  680. begin
  681.   Result := False;
  682.  
  683.   { If the targets properties contains exclude targets checks them first }
  684.  
  685.   if FExcludeCount > 0 then
  686.   begin
  687.     for i := 0 to Count - 1 do
  688.     begin
  689.       target := Items[i];
  690.       if (target.TargetType = ivttExclude) and DoesMatch then
  691.         Exit;
  692.     end;
  693.   end;
  694.  
  695.   { Checks if any include target matches the give property }
  696.  
  697.   for i := 0 to Count - 1 do
  698.   begin
  699.     target := Items[i];
  700.     if (target.TargetType = ivttInclude) and DoesMatch then
  701.     begin
  702.       Result := True;
  703.       Exit;
  704.     end;
  705.   end;
  706. end;
  707.  
  708.  
  709. { TIvBidirectionalState }
  710.  
  711. constructor TIvBidirectionalState.Create(const control: TControl);
  712. begin
  713.   inherited Create;
  714. end;
  715.  
  716.  
  717. { TIvTranslatorTable }
  718.  
  719. constructor TIvTranslatorTableItem.Create(obj: TObject; const component, propertyName, native, current: String);
  720. begin
  721.   inherited Create;
  722.   Self.obj := obj;
  723.   Self.component := component;
  724.   Self.propertyName := propertyName;
  725.   Self.native := native;
  726.   Self.current := current;
  727. end;
  728.  
  729. constructor TIvTranslatorTable.Create;
  730. begin
  731.   inherited Create;
  732.   FRows := TList.Create;
  733. end;
  734.  
  735. destructor TIvTranslatorTable.Destroy;
  736. begin
  737.   Clear;
  738.   FRows.Free;
  739.   inherited Destroy;
  740. end;
  741.  
  742. procedure TIvTranslatorTable.Clear;
  743. var
  744.   i: Integer;
  745. begin
  746.   for i := 0 to FRows.Count - 1 do
  747.     TIvTranslatorTableItem(FRows[i]).Free;
  748.   FRows.Clear;
  749. end;
  750.  
  751. function TIvTranslatorTable.GetCount: Integer;
  752. begin
  753.   Result := FRows.Count;
  754. end;
  755.  
  756. function TIvTranslatorTable.GetItem(row: Integer): TIvTranslatorTableItem;
  757. begin
  758.   Result := FRows[row];
  759. end;
  760.  
  761. procedure TIvTranslatorTable.Add(item: TIvTranslatorTableItem);
  762. begin
  763.   FRows.Add(item);
  764. end;
  765.  
  766.  
  767. { TIvModule }
  768.  
  769. function TIvModule.TranslateComponent(
  770.   translator: TIvTranslator;
  771.   component: TComponent): Boolean;
  772. begin
  773.   Result := False;
  774. end;
  775.  
  776. function TIvModule.FlipControl(
  777.   translator: TIvTranslator;
  778.   control: TControl;
  779.   state: TIvBidirectionalState): Boolean;
  780. begin
  781.   Result := False;
  782. end;
  783.  
  784. function TIvModule.UnFlipControl(
  785.   translator: TIvTranslator;
  786.   control: TControl;
  787.   state: TIvBidirectionalState): Boolean;
  788. begin
  789.   Result := False;
  790. end;
  791.  
  792. function TIvModule.ChangeComponentReadingOrder(
  793.   translator: TIvTranslator;
  794.   component: TComponent): Boolean;
  795. begin
  796.   Result := False;
  797. end;
  798.  
  799.  
  800. { TIvModules }
  801.  
  802. constructor TIvModules.Create;
  803. begin
  804.   inherited Create;
  805.   FItems := TList.Create;
  806. end;
  807.  
  808. destructor TIvModules.Destroy;
  809. begin
  810.   FItems.Free;
  811.   inherited Destroy;
  812. end;
  813.  
  814. function TIvModules.GetCount: Integer;
  815. begin
  816.   Result := FItems.Count;
  817. end;
  818.  
  819. function TIvModules.GetItems(index: Integer): TIvModule;
  820. begin
  821.   Result := FItems[index];
  822. end;
  823.  
  824. procedure TIvModules.Add(item: TIvModule);
  825. var
  826.   i: Integer;
  827. begin
  828.   { Adds the module the the module list if it is not already there. }
  829.  
  830.   for i := 0 to Count - 1 do
  831.     if Items[i].ClassType = item.ClassType then
  832.       Exit;
  833.  
  834.   FItems.Add(item);
  835. end;
  836.  
  837. function TIvModules.TranslateComponent(
  838.   translator: TIvTranslator;
  839.   component: TComponent): Boolean;
  840. var
  841.   i: Integer;
  842. begin
  843.   Result := False;
  844.   for i := 0 to Count - 1 do
  845.     if Items[i].TranslateComponent(translator, component) then
  846.     begin
  847.       Result := True;
  848.       Break;
  849.     end;
  850. end;
  851.  
  852.  
  853.  
  854. { TIvTranslator }
  855.  
  856. constructor TIvTranslator.Create(owner: TComponent);
  857. var
  858.   detectType: TIvDetectType;
  859. begin
  860.   inherited Create(owner);
  861.  
  862.   FNativeLayout := ivroLeftToRight;
  863.   FHost := nil;
  864.   FTranslatorTable := TIvTranslatorTable.Create;
  865.   FTargets := TIvTargetProperties.Create;
  866.  
  867.   FOptions := [ivtoAutoTranslate, ivtoCheckFont, ivtoScaleMultiByte,
  868.     ivtoMirrorBiDirectional, ivtoChangeFontCharset, ivtoAutoOpen, ivtoTranslateSystemMenu];
  869.  
  870.   FMultiByteScale := 100;
  871.  
  872.  
  873.   if IsDesignTime and not (csReading in owner.ComponentState) and not (csLoading in owner.ComponentState) then
  874.   begin
  875.     detectType :=
  876.       TIvDetectType(StrToIntDef(GetMLRegistryValue(DETECT_C, IntToStr(Integer(ivdtEnabled))), Integer(ivdtEnabled)));
  877.     if (detectType = ivdtEnabled) or ((detectType = ivdtPrompt) and
  878.       (MessageDlg(
  879.         'Do you want the translator component to detect the default targets?',
  880.         mtInformation,
  881.         [mbYes, mbNo],
  882.         0) = mrYes)) then
  883.     begin
  884.       DetectTargets(FTargets, False);
  885.     end;
  886.   end;
  887. end;
  888.  
  889. destructor TIvTranslator.Destroy;
  890. begin
  891.   Unbind;
  892.   FTranslatorTable.Free;
  893.   FTargets.Free;
  894.   inherited Destroy;
  895. end;
  896.  
  897. function TIvTranslator.GetAutoOpen: Boolean;
  898. begin
  899.   Result := ivtoAutoOpen in FOptions;
  900. end;
  901.  
  902. procedure TIvTranslator.SetAutoOpen(value: Boolean);
  903. begin
  904.   if value then
  905.     FOptions := FOptions + [ivtoAutoOpen]
  906.   else
  907.     FOptions := FOptions - [ivtoAutoOpen];
  908. end;
  909.  
  910. function TIvTranslator.GetHost: TComponent;
  911. begin
  912.   if FHost <> nil then
  913.     Result := FHost
  914.   else
  915.     Result := Owner;
  916. end;
  917.  
  918. procedure TIvTranslator.SetTargets(value: TIvTargetProperties);
  919. begin
  920.   FTargets.Assign(value);
  921. end;
  922.  
  923. function TIvTranslator.DoTranslateString(
  924.   obj: TObject;
  925.   const propertyName, value: String): String;
  926. begin
  927.   Result := DoTranslateContextString(obj, '', propertyName, value);
  928. end;
  929.  
  930. function TIvTranslator.DoTranslateContextString(
  931.   obj: TObject;
  932.   const component, propertyName, value: String): String;
  933. var
  934.   i: Integer;
  935.   item: TIvTranslatorTableItem;
  936.  
  937.   function Translate(const str, component: String): String;
  938.   var
  939.     l, h, i, c: Integer;
  940.     ok: Boolean;
  941.     thisStr: String;
  942.     translation: TIvTranslation;
  943. {$IFNDEF IVVB}
  944.     thisHost: TClass;
  945. {$ENDIF}
  946.   begin
  947.     if FDictionary = nil then
  948.     begin
  949.       Result := str;
  950.       Exit;
  951.     end;
  952.  
  953.     { Tries to first find from the translation list }
  954.  
  955.     l := 0;
  956.     h := FTranslations.Count - 1;
  957.     thisStr := str + Host.Name + component;
  958.     while l <= h do
  959.     begin
  960.       i := (l + h) div 2;
  961.       translation := TIvTranslation(FTranslations[i]);
  962.       c := SysUtils.CompareStr(translation.Key, thisStr);
  963.       if c = 0 then
  964.       begin
  965.         if translation.Exists then
  966.         begin
  967.           Result := translation.Current;
  968.           if Result = '' then
  969.             Result := str;
  970.         end
  971.         else
  972.           Result := str;
  973.         Exit;
  974.       end
  975.       else if c < 0 then
  976.         l := i + 1
  977.       else
  978.         h := i - 1;
  979.     end;
  980.  
  981.     { Not found. Gets the translation from the dictionary. }
  982.  
  983.     if FDictionary.ContextType = [] then
  984.     begin
  985.       { Flat dictionary }
  986.  
  987.       Result := FDictionary.Translate(str);
  988.     end
  989.     else
  990.     begin
  991.       { Context sensitive dictionary }
  992.  
  993. {$IFDEF IVVB}
  994.       ok := FDictionary.TranslateContextString(str, Host.Name, component, Result);
  995. {$ELSE}
  996.       thisHost := Host.ClassType;
  997.       repeat
  998.         ok := FDictionary.TranslateContextString(str, thisHost.ClassName, component, Result);
  999.         thisHost := thisHost.ClassParent;
  1000.       until ok or (thisHost = TForm) or
  1001.   {$IFDEF IVWIDE}
  1002.         (thisHost = TCustomForm) or
  1003.   {$ENDIF}
  1004.         (thisHost = TComponent);
  1005. {$ENDIF}
  1006.  
  1007.       if ok then
  1008.         Result := FDictionary.CheckTranslation(str, Result, ok)
  1009.       else
  1010.       begin
  1011.         { Could not tranlsate ther string in a context sensitive way.
  1012.           Translates in a flat way. }
  1013.  
  1014.         Result := FDictionary.Translate(str);
  1015.       end;  
  1016.     end;
  1017.   end;
  1018.  
  1019. begin
  1020.   Result := value;
  1021.   if value = '' then
  1022.     Exit;
  1023.  
  1024.   { Scans the translator table to find the string.
  1025.     If finds the string, gets the native string and translates it. }
  1026.  
  1027.   for i := 0 to FTranslatorTable.Count - 1 do
  1028.   begin
  1029.     item := FTranslatorTable.Items[i];
  1030.     if (item.Obj = obj) and
  1031.       (item.PropertyName = propertyName) and
  1032.       (item.Current = value) then
  1033.     begin
  1034.       Result := Translate(item.Native, item.Component);
  1035.       item.Current := Result;
  1036.       Exit;
  1037.     end;
  1038.   end;
  1039.  
  1040.   { Not found. Translates the string and adds it to the table }
  1041.  
  1042.   Result := Translate(value, component);
  1043.   FTranslatorTable.Add(TIvTranslatorTableItem.Create(
  1044.     obj,
  1045.     component,
  1046.     propertyName,
  1047.     value,
  1048.     Result));
  1049. end;
  1050.  
  1051. procedure TIvTranslator.DoTranslateStrings(
  1052.   obj: TObject;
  1053.   const component, propertyName: String; value: TStrings);
  1054. var
  1055.   i: Integer;
  1056. begin
  1057.   if value.Count = 0 then
  1058.     Exit;
  1059.  
  1060.   value.BeginUpdate;
  1061.   try
  1062.     for i := 0 to value.Count - 1 do
  1063.       value[i] := DoTranslateContextString(obj, component, propertyName, value[i]);
  1064.   finally
  1065.     value.EndUpdate;
  1066.   end;  
  1067. end;
  1068.  
  1069. procedure TIvTranslator.AddTranslation(
  1070.   obj: TObject;
  1071.   const component, propertyName, value: String);
  1072. var
  1073.   i: Integer;
  1074.   item: TIvTranslatorTableItem;
  1075. begin
  1076.   for i := 0 to FTranslatorTable.Count - 1 do
  1077.   begin
  1078.     item := FTranslatorTable.Items[i];
  1079.     if (item.Obj = obj) and
  1080.       (item.PropertyName = propertyName) and
  1081.       (item.Current = value) then
  1082.     begin
  1083.       FTranslations.Add(TIvTranslation.CreateValue(item.Native, Host.Name, component));
  1084.       Exit;
  1085.     end;
  1086.   end;
  1087.  
  1088.   FTranslations.Add(TIvTranslation.CreateValue(value, Host.Name, component));
  1089. end;
  1090.  
  1091. procedure TIvTranslator.TranslateObject(obj: TObject; restrictions: TIvRestrictions);
  1092. var
  1093.   found, canTranslate, doTranslate: Boolean;
  1094.   i, j, k, oldIndex, locale: Integer;
  1095.   componentName, str: String;
  1096.   item: PPropInfo;
  1097.   list: TIvPropInfoList;
  1098.   strings: TStrings;
  1099.   objectProperty: TObject;
  1100.   component, ownerComponent: TComponent;
  1101.   filter: TTypeKinds;
  1102.   oldSelected: TList;
  1103. {$IFDEF WIN32}
  1104.   collection: TCollection;
  1105. {$ENDIF}
  1106. begin
  1107.   if (obj is TIvDictionary) or (obj is TIvTranslator) or (obj is TFont) then
  1108.     Exit;
  1109.  
  1110.   { Checks the restriction }
  1111.  
  1112.   canTranslate := True;
  1113.   if Assigned(FOnRestrictObject) then
  1114.     FOnRestrictObject(self, obj, canTranslate);
  1115.   if not canTranslate then
  1116.     Exit;
  1117.  
  1118.   { Gets the properties of the object }
  1119.  
  1120.   filter := [tkClass, tkString
  1121. {$IFDEF WIN32}
  1122.   , tkLString
  1123. {$ENDIF}
  1124.   ];
  1125.  
  1126.   if not (ivtsPreScanning in FState) and (ivtoUpdateLocaleProperty in Options)
  1127.   then
  1128.   begin
  1129.     filter := filter + [tkInteger];
  1130.   end;
  1131.  
  1132.   list := TIvPropInfoList.Create(obj, filter);
  1133.   try
  1134.     componentName := '';
  1135.     if obj is TComponent then
  1136.     begin
  1137.       component := TComponent(obj);
  1138.       if component <> FHost then
  1139.         componentName := component.name;
  1140.     end
  1141.     else
  1142.       component := nil;
  1143.  
  1144.     for i := 0 to list.Count - 1 do
  1145.     begin
  1146.       { Checks if the property is read only.
  1147.         In that case it is not translated }
  1148.  
  1149.       item := list.Items[i];
  1150.       if (item^.SetProc = nil) or (item^.GetProc = nil) then
  1151.         Continue;
  1152.  
  1153.       { Checks if the translation of the property is restricted.
  1154.         In that case it is not translated }
  1155.  
  1156.       canTranslate := True;
  1157.       if Assigned(FOnRestrictProperty) then
  1158.         FOnRestrictProperty(self, obj, item^.Name, canTranslate);
  1159.       if not canTranslate then
  1160.         Continue;
  1161.  
  1162.       case item^.PropType^.Kind of
  1163. {$IFDEF WIN32}
  1164.         tkLString,
  1165. {$ENDIF}
  1166.         tkString:
  1167.         begin
  1168.           { A string property.
  1169.             The Name property of a component is not translated. }
  1170.  
  1171.           if ((component = nil) or (CompareText(item^.Name, 'name') <> 0)) and
  1172.             FTargets.IsPropertyInTargets(obj.ClassName, item^.Name) then
  1173.           begin
  1174.             str := GetStrProp(obj, item);
  1175.             if str <> '' then
  1176.               if ivtsPreScanning in FState then
  1177.                 AddTranslation(obj, componentName, item^.Name, str)
  1178.               else
  1179.                 SetStrProp(
  1180.                   obj,
  1181.                   item,
  1182.                   DoTranslateContextString(obj, componentName, item^.Name, str));
  1183.           end;
  1184.         end;
  1185.  
  1186.         tkInteger:
  1187.         begin
  1188.           { If the object is a locale aware object, updates the Locale property }
  1189.  
  1190.           if (component <> nil) and
  1191.             {(ivtoUpdateLocaleProperty in Options) and}
  1192.             (CompareText(item^.Name, 'locale') = 0) and
  1193.             not (ivrFlip in restrictions) then
  1194.           begin
  1195.             if FDictionary = nil then
  1196.               locale := 0
  1197.             else
  1198.               locale := FDictionary.LanguageLocale;
  1199.             SetOrdProp(obj, item, locale);
  1200.           end;
  1201.         end;
  1202.  
  1203.         tkClass:
  1204.         begin
  1205.           { The property is an embedded class }
  1206.  
  1207.           objectProperty := TObject(GetOrdProp(obj, item));
  1208.  
  1209.           if objectProperty is TStrings then
  1210.           begin
  1211.             { The property is a string list }
  1212.  
  1213.             if FTargets.IsPropertyInTargets(obj.ClassName, item^.Name) then
  1214.             begin
  1215.               strings := TStrings(objectProperty);
  1216.  
  1217.               { Save the selected index if the object is either a list or combo box }
  1218.  
  1219.               doTranslate := True;
  1220.               oldIndex := 0;
  1221.               oldSelected := nil;
  1222.               if obj is TCustomListBox then
  1223.               begin
  1224.                 oldIndex := TCustomListBox(obj).ItemIndex;
  1225.  
  1226.                 if (obj is TListBox) and TListBox(obj).MultiSelect then
  1227.                 begin
  1228.                   oldSelected := TList.Create;
  1229.                   for k := 0 to TListBox(obj).Items.Count - 1 do
  1230.                     oldSelected.Add(Pointer(TListBox(obj).Selected[k]));
  1231.                 end;
  1232. {$IFDEF IVWIDE}
  1233.                 if obj is TCheckListBox then
  1234.                 begin
  1235.                   oldSelected := TList.Create;
  1236.                   for k := 0 to TCheckListBox(obj).Items.Count - 1 do
  1237.                     oldSelected.Add(Pointer(TCheckListBox(obj).Checked[k]));
  1238.                 end;
  1239. {$ENDIF}
  1240.               end
  1241.               else if obj is TCustomComboBox then
  1242.                 with TCustomComboBox(obj) do
  1243.                 begin
  1244.                   doTranslate := Visible;
  1245.                   if Visible then
  1246.                     oldIndex := ItemIndex
  1247.                   else
  1248.                     oldIndex := -1
  1249.                 end;
  1250.  
  1251.               if doTranslate and (obj.ClassName <> 'TOutline') then
  1252.               begin
  1253.                 if ivtsPreScanning in FState then
  1254.                 begin
  1255.                   for k := 0 to strings.Count - 1 do
  1256.                     AddTranslation(obj, componentName, item^.Name, strings[i])
  1257.                 end
  1258.                 else
  1259.                   DoTranslateStrings(obj, componentName, item^.Name, strings);
  1260.               end;
  1261.  
  1262.               { Restores the saved item index of the list or combo box }
  1263.  
  1264.               if obj is TCustomListBox then
  1265.               begin
  1266. {$IFDEF IVWIDE}
  1267.                 if obj is TCheckListBox then
  1268.                   for k := 0 to TCheckListBox(obj).Items.Count - 1 do
  1269.                     TCheckListBox(obj).Checked[k] := Boolean(oldSelected[k]);
  1270. {$ENDIF}
  1271.                 if (obj is TListBox) and TListBox(obj).MultiSelect then
  1272.                   for k := 0 to TListBox(obj).Items.Count - 1 do
  1273.                     TListBox(obj).Selected[k] := Boolean(oldSelected[k]);
  1274.  
  1275.                 if oldIndex <> -1 then
  1276.                   TCustomListBox(obj).ItemIndex := oldIndex
  1277.               end
  1278.               else if obj is TCustomComboBox then
  1279.               begin
  1280.                 if oldIndex <> -1 then
  1281.                   TCustomComboBox(obj).ItemIndex := oldIndex;
  1282.               end;
  1283.  
  1284.               oldSelected.Free;
  1285.             end;
  1286.           end
  1287. {$IFDEF WIN32}
  1288.           else if objectProperty is TCollection then
  1289.           begin
  1290.             { The property is a collection }
  1291.  
  1292.             collection := TCollection(objectProperty);
  1293.             for j := 0 to collection.Count - 1 do
  1294.               TranslateObject(collection.Items[j], restrictions);
  1295.           end
  1296. {$ENDIF}
  1297. {$IFDEF IVWIDE}
  1298.           else if objectProperty is TFont then
  1299.           begin
  1300.             { Changes the charset of the font object
  1301.               Do not change TColumnTitle.Font. This is because a bug in VCL that
  1302.               causes the application exception if the charset of TColumnTitle
  1303.               is changed. }
  1304.  
  1305.             if (FDictionary <> nil) and
  1306.               (ivtoChangeFontCharset in FOptions) and
  1307.               not (ivrCharset in restrictions) and
  1308.               (obj.ClassName <> 'TColumnTitle') then
  1309.             begin
  1310.               { Changing of the font character set requires an allocated
  1311.                 windows handle. }
  1312.  
  1313.               if (obj is TWinControl) and not TWinControl(obj).HandleAllocated then
  1314.               begin
  1315.                 doTranslate := (TWinControl(obj).Parent <> nil) and (TWinControl(obj).Parent.HandleAllocated);
  1316.                 if doTranslate then
  1317.                 try
  1318.                   TWinControl(obj).HandleNeeded;
  1319.                 except
  1320.                   doTranslate := False;
  1321.                 end;
  1322.               end
  1323.               else
  1324.                 doTranslate := True;
  1325.  
  1326.               if doTranslate and (TFont(objectProperty).Charset <> Dictionary.LanguageData.Charset) then
  1327.                 TFont(objectProperty).Charset := Dictionary.LanguageData.Charset;
  1328.             end;
  1329.           end
  1330. {$ENDIF}
  1331.           else if objectProperty is TComponent then
  1332.           begin
  1333.             if component <> nil then
  1334.             begin
  1335.               { The property is another component.
  1336.                 Scans the components of the owner component. If finds the same
  1337.                 component does not translate the component. }
  1338.  
  1339.               found := False;
  1340.               ownerComponent := component.Owner;
  1341.               if ownerComponent <> nil then
  1342.               begin
  1343.                 for j := 0 to ownerComponent.ComponentCount - 1 do
  1344.                 begin
  1345.                   if objectProperty = ownerComponent.Components[j] then
  1346.                   begin
  1347.                     found := True;
  1348.                     Break;
  1349.                   end;
  1350.                 end;
  1351.               end;
  1352.  
  1353.               if not found then
  1354.                 TranslateObject(objectProperty, restrictions);
  1355.             end;
  1356.           end
  1357.           else if objectProperty <> nil then
  1358.             TranslateObject(objectProperty, restrictions);
  1359.         end;
  1360.       end;
  1361.     end;
  1362.   finally
  1363.     list.Free;
  1364.   end;
  1365. end;
  1366.  
  1367. procedure TIvTranslator.Translate;
  1368. begin
  1369.   if ivtsBound in FState then
  1370.     TranslateHost
  1371.   else
  1372.   begin
  1373.     if FDictionary = nil then
  1374.     begin
  1375.       { If there is not dictionaris in the application, raises an exception.
  1376.         If the dictionary name was given, finds a dictionary that matches the
  1377.         name.
  1378.         If no name was given or the name was not found uses the first
  1379.         dictionary. }
  1380.  
  1381.       if Dictionaries.Count = 0 then
  1382.       begin
  1383.         { We don't want to throw an exception at design time }
  1384.  
  1385.         if IsDesignTime then
  1386.           Exit;
  1387.         raise EIvMulti.Create(Format(NO_DICTIONARY_C, [Name, Host.Name]));
  1388.       end;
  1389.  
  1390.       if FDictionaryName <> '' then
  1391.         FDictionary := Dictionaries.FindDictionary(FDictionaryName);
  1392.  
  1393.       if FDictionary = nil then
  1394.         FDictionary := Dictionaries[0];
  1395.     end;
  1396.  
  1397.     if FDictionary.IsDesignTime or (not FDictionary.CanBeOpened) or (FTargets.Count = 0) then
  1398.       Exit;
  1399.  
  1400.     if not FDictionary.IsOpen then
  1401.       FDictionary.Open;
  1402.  
  1403. {$IFDEF IVVB}
  1404.     if Host is TComponentOCX then
  1405.     begin
  1406.       FDefaultFontName := TComponentOCX(Host).FontName;
  1407.       FDefaultFontSize := TComponentOCX(Host).FontSize;
  1408.     end
  1409.     else
  1410. {$ENDIF}
  1411.     begin
  1412. {$IFDEF IVWIDE}
  1413.       if Host is TCustomForm then
  1414.       begin
  1415.         FDefaultFontName := TCustomForm(Host).Font.Name;
  1416.         FDefaultFontSize := TCustomForm(Host).Font.Size;
  1417.       end
  1418. {$ELSE}
  1419.       if Host is TForm then
  1420.       begin
  1421.         FDefaultFontName := TForm(Host).Font.Name;
  1422.         FDefaultFontSize := TForm(Host).Font.Size;
  1423.       end
  1424. {$ENDIF}
  1425.       else
  1426.       begin
  1427.         FDefaultFontName := '';
  1428.         FDefaultFontSize := 0;
  1429.       end;
  1430.     end;
  1431.  
  1432.     if FHost = nil then
  1433.       FHost := Owner;
  1434.     TranslateHost;
  1435.     LanguageChanged(True, True);
  1436.     FDictionary.AddTranslator(Self);
  1437.   end;
  1438.  
  1439.   inherited Translate;
  1440. end;
  1441.  
  1442. procedure TIvTranslator.Unbind;
  1443. begin
  1444.   inherited Unbind;
  1445.   FTranslatorTable.Clear;
  1446.   FHost := nil;
  1447. end;
  1448.  
  1449. procedure TIvTranslator.UnbindAndSetNative;
  1450. begin
  1451.   if ivtsBound in FState then
  1452.   begin
  1453.     inherited Unbind;
  1454.     TranslateHost;
  1455.   end;
  1456.   inherited UnbindAndSetNative;
  1457. end;
  1458.  
  1459. procedure TIvTranslator.Open(host: TComponent);
  1460. begin
  1461.   FHost := host;
  1462.   Translate;
  1463. end;
  1464.  
  1465. procedure TIvTranslator.Close;
  1466. var
  1467.   oldLocale: Integer;
  1468.   oldOptions: TIvDictionaryOptions;
  1469. begin
  1470.   if FDictionary <> nil then
  1471.   begin
  1472.     oldOptions := FDictionary.Options;
  1473.     oldLocale := FDictionary.Locale;
  1474.     FDictionary.Options := FDictionary.Options - [ivdoAutoTranslate];
  1475.     FDictionary.Language := 0;
  1476.     Translate;
  1477.     FDictionary.Locale := oldLocale;
  1478.     FDictionary.Options := oldOptions;
  1479.   end;
  1480.   Unbind;
  1481. end;
  1482.  
  1483. function TIvTranslator.GetNativePropertyValue(
  1484.   component: TObject;
  1485.   const propertyName: String): String;
  1486. var
  1487.   i: Integer;
  1488. begin
  1489.   for i := 0 to FTranslatorTable.Count - 1 do
  1490.   begin
  1491.     if (FTranslatorTable.Items[i].Obj = component) and
  1492.       (CompareText(FTranslatorTable.Items[i].PropertyName, propertyName) = 0) then
  1493.     begin
  1494.       Result := FTranslatorTable.Items[i].Native;
  1495.       Exit;
  1496.     end;
  1497.   end;
  1498.  
  1499.   raise Exception.Create('Property has not been translated');
  1500. end;
  1501.  
  1502. function TIvTranslator.GetNativePropertyValueEx(
  1503.   component: TObject;
  1504.   const propertyName, currentValue: String): String;
  1505. var
  1506.   i: Integer;
  1507. begin
  1508.   for i := 0 to FTranslatorTable.Count - 1 do
  1509.   begin
  1510.     if (FTranslatorTable.Items[i].Obj = component) and
  1511.       (CompareText(FTranslatorTable.Items[i].PropertyName, propertyName) = 0) and
  1512.       (CompareText(FTranslatorTable.Items[i].Current, currentValue) = 0) then
  1513.     begin
  1514.       Result := FTranslatorTable.Items[i].Native;
  1515.       Exit;
  1516.     end;
  1517.   end;
  1518.  
  1519.   raise Exception.Create('Property has not been translated');
  1520. end;
  1521.  
  1522. procedure TIvTranslator.LanguageChanged(languageChanged, localeChanged: Boolean);
  1523. begin
  1524.   if (ivtsBound in FState) and (ivtoAutoTranslate in FOptions) then
  1525.     Translate;
  1526.  
  1527.   if languageChanged and Assigned(FOnLanguageChange) then
  1528.     FOnLanguageChange(Self);
  1529.  
  1530.   if localeChanged and Assigned(FOnLocaleChange) then
  1531.     FOnLocaleChange(Self);
  1532. end;
  1533.  
  1534. procedure TIvTranslator.CheckHostFont;
  1535. {$IFDEF IVANSI}
  1536. const
  1537.   MICROSOFT_SANS_SERIF_FONT_C = 'Microsoft Sans Serif';
  1538.   MS_SANS_SERIF_FONT_C = 'MS Sans Serif';
  1539.   MS_SYSTEM_FONT_C = 'System';
  1540.   ARIAL_FONT_C = 'Arial';
  1541. {$ENDIF}
  1542. var
  1543.   fontSize: Integer;
  1544.   fontName: String;
  1545. {$IFDEF IVANSI}
  1546.   names: TStringList;
  1547. {$ENDIF}
  1548.  
  1549. {$IFDEF IVANSI}
  1550.   function DoesListContainFontname(const value: String): Boolean;
  1551.   var
  1552.     i: Integer;
  1553.   begin
  1554.     Result := False;
  1555.     for i := 0 to names.Count - 1 do
  1556.       if CompareText(names[i], value) = 0 then
  1557.       begin
  1558.         Result := True;
  1559.         Exit;
  1560.       end;
  1561.   end;
  1562. {$ENDIF}
  1563.  
  1564. begin
  1565.   { Checks first the custom font name of the current language. If it is
  1566.     not given the original font name of the host is used. }
  1567.  
  1568.   fontName := FDictionary.LanguageData.FontName;
  1569.   if fontName = '' then
  1570.     fontName := FDefaultFontName;
  1571.  
  1572.   fontSize := FDictionary.LanguageData.FontSize;
  1573.   if fontSize = 0 then
  1574.     fontSize := FDefaultFontSize;
  1575.  
  1576. {$IFDEF IVANSI}
  1577.   { Delphi 2.0 and C++Builder 1.0 do not have support for font character sets.
  1578.     That's why the font to be used must support the current character set-
  1579.     If it do not match the following fonts are tried:
  1580.     'Microsoft Sanf Serif' -> 'MS Sanf Serif' -> 'System' -> 'Arial' in this order.
  1581.     If none of above matches the first font that support the character set is
  1582.     used. }
  1583.  
  1584.   names := TStringList.Create;
  1585.   try
  1586.     IvGetFontNames(
  1587.       [IvCodeToCharset(IvLangIdToCharset(FDictionary.Locale))],
  1588.       names);
  1589.  
  1590.     if DoesListContainFontname(fontName) then
  1591.       fontName := fontName
  1592.     else if DoesListContainFontname(FDefaultFontName) then
  1593.       fontName := FDefaultFontName
  1594.     else if DoesListContainFontname(MICROSOFT_SANS_SERIF_FONT_C) then
  1595.       fontName := MICROSOFT_SANS_SERIF_FONT_C
  1596.     else if DoesListContainFontname(MS_SANS_SERIF_FONT_C) then
  1597.       fontName := MS_SANS_SERIF_FONT_C
  1598.     else if DoesListContainFontname(MS_SYSTEM_FONT_C) then
  1599.       fontName := MS_SYSTEM_FONT_C
  1600.     else if DoesListContainFontname(ARIAL_FONT_C) then
  1601.       fontName := ARIAL_FONT_C
  1602.     else if names.Count > 0 then
  1603.       fontName := names[0];
  1604.   finally
  1605.     names.Free;
  1606.   end;
  1607. {$ENDIF}
  1608.  
  1609.   { Sets the new font name, size and charset for the host form }
  1610.  
  1611. {$IFDEF IVWIDE}
  1612.   if (Host is TCustomForm) then
  1613.   begin
  1614.     with TCustomForm(Host) do
  1615.     begin
  1616.       if HandleAllocated then
  1617.       begin
  1618.         if fontName <> Font.Name then
  1619.           Font.Name := fontName;
  1620.  
  1621.         if fontSize <> Font.Size then
  1622.           Font.Size := fontSize;
  1623.  
  1624.         {if (FDictionary.LanguageData.Charset <> Font.Charset) and (FDictionary.LanguageData.Charset <> ANSI_CHARSET) then}
  1625.         if FDictionary.LanguageData.Charset <> Font.Charset then
  1626.           Font.Charset := FDictionary.LanguageData.Charset;
  1627.       end;
  1628.     end;
  1629.   end;
  1630. {$ELSE}
  1631.   if (Host is TForm) then
  1632.   begin
  1633.     with TForm(Host) do
  1634.     begin
  1635.       if HandleAllocated then
  1636.       begin
  1637.         if fontName <> Font.Name then
  1638.           Font.Name := fontName;
  1639.  
  1640.         if fontSize <> Font.Size then
  1641.           Font.Size := fontSize;
  1642.       end;
  1643.     end;
  1644.   end;
  1645. {$ENDIF}
  1646. end;
  1647.  
  1648. procedure TIvTranslator.TranslateHost;
  1649. var
  1650.   mdi: Boolean;
  1651. {$IFDEF IVVB}
  1652.   vbFormHost: boolean;
  1653. {$ENDIF}
  1654.  
  1655.   procedure QuickSort(left, right: Integer);
  1656.   var
  1657.     i, j: Integer;
  1658.     p: String;
  1659.     translation: TIvTranslation;
  1660.   begin
  1661.     i := left;
  1662.     j := right;
  1663.     p := TIvTranslation(FTranslations[(left + right) shr 1]).Key;
  1664.  
  1665.     repeat
  1666.       while SysUtils.CompareStr(TIvTranslation(FTranslations[i]).Key, p) < 0 do
  1667.         Inc(i);
  1668.       while SysUtils.CompareStr(TIvTranslation(FTranslations[j]).Key, p) > 0 do
  1669.         Dec(j);
  1670.  
  1671.       if i <= j then
  1672.       begin
  1673.         translation := FTranslations[i];
  1674.         FTranslations[i] := FTranslations[j];
  1675.         FTranslations[j] := translation;
  1676.         Inc(i);
  1677.         Dec(j);
  1678.       end;
  1679.     until i > j;
  1680.  
  1681.     if left < j then
  1682.       QuickSort(left, j);
  1683.  
  1684.     if i < right then
  1685.       QuickSort(i, right);
  1686.   end;
  1687.  
  1688. begin
  1689.   inherited TranslateHost;
  1690.  
  1691.   if Host is TForm then
  1692.     mdi := TForm(Host).FormStyle = fsMDIChild
  1693.   else
  1694.     mdi := False;
  1695.  
  1696.   { If the dictionary uses multiple translation, prescans the from to get the
  1697.     translations. }
  1698.  
  1699.   if FDictionary <> nil then
  1700.   begin
  1701.     if Dictionary.GetTranslationMode = ivtmMultiple then
  1702.     begin
  1703.       FState := FState + [ivtsPreScanning];
  1704.       try
  1705.         ClearTranslations;
  1706.         TranslateSubComponent(Host, []);
  1707. {$IFDEF WIN32}
  1708.         if ivtoTranslateSystemMenu in FOptions then
  1709.           TranslateSystemMenu(GetSystemMenuWinHandle, mdi);
  1710. {$ENDIF}
  1711.         if FTranslations.Count > 0 then
  1712.         begin
  1713.           QuickSort(0, FTranslations.Count - 1);
  1714.           Dictionary.TranslateStrings(FTranslations);
  1715.          end;
  1716.       finally
  1717.         FState := FState - [ivtsPreScanning];
  1718.       end;
  1719.     end;
  1720.  
  1721.   end;
  1722.  
  1723.   if Assigned(FOnBeforeTranslate) then
  1724.     FOnBeforeTranslate(Self);
  1725.  
  1726. {$IFDEF IVVB}
  1727.   vbFormHost := ((Host is TComponentOCX) and (TComponentOCX(Host).IsFormObject));
  1728. {$ENDIF}
  1729.  
  1730.   { Check the host font }
  1731.  
  1732.   if (FDictionary <> nil) and (ivtoCheckFont in FOptions) and (
  1733. {$IFDEF IVVB}
  1734.     (vbFormHost = TRUE) or
  1735. {$ENDIF}
  1736. {$IFDEF IVVB}
  1737.     ((Host is TComponentOCX) and (TComponentOCX(Host).IsFormObject)) or
  1738. {$ENDIF}
  1739. {$IFDEF IVWIDE}
  1740.     (Host is TCustomForm)) then
  1741. {$ELSE}
  1742.     (Host is TForm)) then
  1743. {$ENDIF}
  1744.   begin
  1745.     CheckHostFont;
  1746.   end;
  1747.  
  1748.   { Checks that the font of the host component supports the current language.
  1749.     Translates the host component }
  1750.  
  1751.   TranslateSubComponent(Host, []);
  1752.  
  1753. {$IFDEF WIN32}
  1754.   if ivtoTranslateSystemMenu in FOptions then
  1755.     TranslateSystemMenu(GetSystemMenuWinHandle, mdi);
  1756. {$ENDIF}
  1757.  
  1758.  
  1759.  
  1760.   if Assigned(FOnAfterTranslate) then
  1761.     FOnAfterTranslate(Self);
  1762. end;
  1763.  
  1764. function TIvTranslator.GetBidirectionalState(control: TControl; allocNew: Boolean): TIvBidirectionalState;
  1765. begin
  1766.   Result := nil;
  1767. end;
  1768.  
  1769. procedure TIvTranslator.UpdateControl(control: TControl);
  1770. begin
  1771. end;
  1772.  
  1773. procedure TIvTranslator.UpdateControls;
  1774. begin
  1775. end;
  1776.  
  1777. procedure TIvTranslator.FlipControl(control: TControl; state: TIvBidirectionalState);
  1778. begin
  1779. end;
  1780.  
  1781. procedure TIvTranslator.UnFlipControl(control: TControl; state: TIvBidirectionalState);
  1782. begin
  1783. end;
  1784.  
  1785. procedure TIvTranslator.TranslateComponent(component: TComponent);
  1786. var
  1787.   str: String;
  1788. begin
  1789.   Modules.TranslateComponent(Self, component);
  1790.  
  1791.   if (component is TCustomMemo) then
  1792.   begin
  1793.     if FTargets.IsPropertyInTargets(component.ClassName, 'Text') then
  1794.       with TCustomMemo(component) do
  1795.       begin
  1796.         str := Text;
  1797.         while (str <> '') and ((str[Length(str)] = #10) or (str[Length(str)] = #13)) do
  1798.           Delete(str, Length(str), 1);
  1799.  
  1800.         if ivtsPreScanning in FState then
  1801.           AddTranslation(
  1802.             component,
  1803.             component.Name,
  1804.             'Text',
  1805.             str)
  1806.         else
  1807.           Text := DoTranslateContextString(
  1808.             component,
  1809.             component.Name,
  1810.             'Text',
  1811.             str);
  1812.       end;
  1813.   end;
  1814. end;
  1815.  
  1816. procedure TIvTranslator.TranslateSubComponent(
  1817.   component: TComponent;
  1818.   parentRestrictions: TIvRestrictions);
  1819. var
  1820.   i: Integer;
  1821.   restrictions, childRestrictions: TIvRestrictions;
  1822.   subComponent: TComponent;
  1823. begin
  1824.   if ivtsPreScanning in FState then
  1825.     childRestrictions := []
  1826.   else
  1827.   begin
  1828.     { Gets the restriction for the component }
  1829.  
  1830.     restrictions := parentRestrictions;
  1831.     if Assigned(FOnRestrictComponent) then
  1832.       FOnRestrictComponent(Self, component, restrictions);
  1833.  
  1834.     { Translates the sub components }
  1835.  
  1836.     if ivrThisComponentOnly in restrictions then
  1837.       childRestrictions := []
  1838.     else
  1839.       childRestrictions := restrictions;
  1840.   end;
  1841.  
  1842.   for i := 0 to component.ComponentCount - 1 do
  1843.   begin
  1844.     subComponent := component.Components[i];
  1845. {$IFDEF IVWIDE}
  1846.     if not (subComponent is TCustomForm) then
  1847. {$ELSE}
  1848.     if not (subComponent is TForm) then
  1849. {$ENDIF}
  1850.       TranslateSubComponent(subComponent, childRestrictions);
  1851.   end;
  1852.  
  1853.   { Performs build in translation }
  1854.  
  1855.   if not (ivrBuildInTranslation in restrictions) then
  1856.     TranslateObject(component, restrictions);
  1857.  
  1858.   { Performs custom translation }
  1859.  
  1860.   if not (ivrCustomTranslation in restrictions) then
  1861.     TranslateComponent(component);
  1862.  
  1863.   if ivtsPreScanning in FState then
  1864.     Exit;
  1865.  
  1866.  
  1867.   if Assigned(FOnTranslate) then
  1868.     FOnTranslate(Self, component);
  1869.  
  1870.   if component is TControl then
  1871.     TControl(component).Invalidate;
  1872. end;
  1873.  
  1874. procedure TIvTranslator.ChangeComponentReadingOrder(component: TComponent);
  1875. begin
  1876. end;
  1877.  
  1878. procedure TIvTranslator.DetectTargets(
  1879.   targets: TIvTargetProperties;
  1880.   prompt: Boolean);
  1881. var
  1882.   i: Integer;
  1883. begin
  1884.   { Scans all the sub componets adding the targets they need }
  1885.  
  1886.   AddObjectTargets(Host, targets, prompt);
  1887.   for i := 0 to Host.ComponentCount - 1 do
  1888.     AddObjectTargets(Host.Components[i], targets, prompt);
  1889. end;
  1890.  
  1891. procedure TIvTranslator.AddObjectTargets(
  1892.   obj: TObject;
  1893.   targets: TIvTargetProperties;
  1894.   prompt: Boolean);
  1895. const
  1896.   DEFAULT_TARGET_COUNT_C = 6;
  1897.   DEFAULT_TARGETS_C: array[0..DEFAULT_TARGET_COUNT_C - 1] of String =
  1898.   (
  1899.     'Caption',
  1900.     'Hint',
  1901.     'Items',
  1902.     'Lines',
  1903.     'Hints',
  1904.     'Tabs'
  1905.   );
  1906. var
  1907.   i, j: Integer;
  1908.   found: Boolean;
  1909.   item: PPropInfo;
  1910.   list: TIvPropInfoList;
  1911.   objectProperty: TObject;
  1912.   availableTargets: TIvTargetProperties;
  1913.   fileName, line, objectName, propertyName: String;
  1914.   f: TextFile;
  1915.   parser: TIvStringParser;
  1916.   component, ownerComponent: TComponent;
  1917.  
  1918.   procedure Add(obj: TObject; name: String; check: Boolean);
  1919.   var
  1920.     id: Integer;
  1921. {$IFNDEF WIN32}
  1922.     buffer: array[0..255] of Char;
  1923. {$ENDIF}
  1924.   begin
  1925.     { If the property has been excluded it is not added }
  1926.  
  1927.     if targets.IsPropertyExcluded(obj.ClassName, name) then
  1928.       Exit;
  1929.  
  1930.     { The property is added if it is included into the avaiable targets and
  1931.       it is not included to the targets }
  1932.  
  1933.     if (not check or (check and (availableTargets <> nil) and availableTargets.IsPropertyInTargets(obj.ClassName, name))) and
  1934.       not targets.IsPropertyInTargets(obj.ClassName, name) then
  1935.     begin
  1936.       if prompt then
  1937.         id := MessageBox(
  1938.           0,
  1939. {$IFDEF WIN32}PChar({$ELSE}StrPCopy(buffer,{$ENDIF}
  1940.           'The translator can translate the ' + obj.ClassName + '.' + name +
  1941.             ' property and all the other "' + name + '" properties on the "' + Host.Name + '" form.'#13#10 +
  1942.             'Do you want to add them to the targets?'),
  1943.           'A new target property found',
  1944.           MB_YESNOCANCEL)
  1945.       else
  1946.         id := IDYES;
  1947.  
  1948.       case id of
  1949.         IDYES: targets.Add(TIvTargetProperty.Create('', name, ivttInclude));
  1950.         IDNO: targets.Add(TIvTargetProperty.Create(obj.ClassName, name, ivttExclude));
  1951.       end;
  1952.     end;
  1953.   end;
  1954.  
  1955. begin
  1956.   { Do not scan dictionary or translator objects }
  1957.  
  1958.   if (obj is TIvDictionary) or (obj is TIvTranslator) then
  1959.     Exit;
  1960.  
  1961.   { Adds the complex targets }
  1962.  
  1963.   if (obj.ClassName = 'TStringGrid') then
  1964.     Add(obj, 'Cells', False);
  1965.   if (obj.ClassName = 'TOutline') then
  1966.     Add(obj, 'Lines', False);
  1967. {$IFDEF WIN32}
  1968.   if (obj.ClassName = 'TTreeView') then
  1969.     Add(obj, 'Items', False);
  1970.   if (obj.ClassName = 'TListView') then
  1971.   begin
  1972.     Add(obj, 'Items', False);
  1973.     Add(obj, 'Caption', False);
  1974.   end;
  1975. {$ENDIF}
  1976.  
  1977.   { Gets the available targets }
  1978.  
  1979.   fileName := GetMLRegistryValue(ROOT_DIR_C, '.') + '\' + TARGETS_C;
  1980.   availableTargets := TIvTargetProperties.Create;
  1981.   try
  1982.     { Reads the available targets from the TARGET.TXT file }
  1983.  
  1984.     if (fileName <> '') and FileExists(fileName) then
  1985.     begin
  1986.       parser := TIvStringParser.CreateValue('', ',');
  1987.       try
  1988.         AssignFile(f, fileName);
  1989.         Reset(f);
  1990.         try
  1991.           while not Eof(f) do
  1992.           begin
  1993.             Readln(f, line);
  1994.             if (line = '') or (line[1] = ';') or (line[1] = ':') then
  1995.               Continue;
  1996.  
  1997.             parser.Value := line;
  1998.             objectName := parser.GetString;
  1999.             propertyName := parser.GetString;
  2000.             availableTargets.Add(TIvTargetProperty.Create(objectName, propertyName, ivttInclude));
  2001.           end;
  2002.         finally
  2003.           CloseFile(f);
  2004.         end;
  2005.       finally
  2006.         parser.Free;
  2007.       end;
  2008.     end;
  2009.  
  2010.     { If the file does not exists or is null uses the default targets }
  2011.  
  2012.     if availableTargets.Count = 0 then
  2013.     begin
  2014.       for i := 0 to DEFAULT_TARGET_COUNT_C - 1 do
  2015.         availableTargets.Add(TIvTargetProperty.Create('', DEFAULT_TARGETS_C[i], ivttInclude));
  2016.     end;
  2017.  
  2018.     { If available targets contains items, scan the componet to find those
  2019.       targets. }
  2020.  
  2021.     list := TIvPropInfoList.Create(
  2022.       obj,
  2023.       [tkClass, tkString{$IFDEF WIN32}, tkLString{$ENDIF}]);
  2024.  
  2025.     try
  2026.       for i := 0 to list.Count - 1 do
  2027.       begin
  2028.         { Checks if the property is read only.
  2029.           In that case it is not translated }
  2030.  
  2031.         item := list.Items[i];
  2032.         if (item^.SetProc = nil) or (item^.GetProc = nil) then
  2033.           Continue;
  2034.  
  2035.         case item^.PropType^.Kind of
  2036. {$IFDEF WIN32}
  2037.           tkLString,
  2038. {$ENDIF}
  2039.           tkString:
  2040.           begin
  2041.             Add(obj, item^.Name, True);
  2042.           end;
  2043.  
  2044.           tkClass:
  2045.           begin
  2046.             { The property is an embedded class }
  2047.  
  2048.             try
  2049.               objectProperty := TObject(GetOrdProp(obj, item));
  2050.  
  2051.               if availableTargets.IsObjectInTargets(objectProperty.ClassName) then
  2052.               begin
  2053.                 if not targets.IsObjectInTargets(objectProperty.ClassName) then
  2054.                   targets.Add(TIvTargetProperty.Create(objectProperty.ClassName, '', ivttInclude));
  2055.               end
  2056.               else if objectProperty is TStrings then
  2057.                 Add(obj, item^.Name, True)
  2058. {$IFDEF WIN32}
  2059.               else if objectProperty is TCollection then
  2060.               begin
  2061.                 { The property is a collection }
  2062.  
  2063.                 with TCollection(objectProperty) do
  2064.                   for j := 0 to Count - 1 do
  2065.                     AddObjectTargets(Items[j], targets, prompt);
  2066.               end
  2067. {$ENDIF}
  2068.               else if objectProperty is TComponent then
  2069.               begin
  2070.                 if obj is TComponent then
  2071.                 begin
  2072.                   component := TComponent(obj);
  2073.  
  2074.                   { The property is another component.
  2075.                     Scans the components of the owner component to avoid
  2076.                     circular references. }
  2077.  
  2078.                   found := False;
  2079.                   ownerComponent := component.Owner;
  2080.                   if ownerComponent <> nil then
  2081.                   begin
  2082.                     for j := 0 to ownerComponent.ComponentCount - 1 do
  2083.                     begin
  2084.                       if objectProperty = ownerComponent.Components[j] then
  2085.                       begin
  2086.                         found := True;
  2087.                         Break;
  2088.                       end;
  2089.                     end;
  2090.                   end;
  2091.  
  2092.                   if not found then
  2093.                     AddObjectTargets(objectProperty, targets, prompt);
  2094.                 end;
  2095.               end
  2096.               else if objectProperty <> nil then
  2097.                 AddObjectTargets(objectProperty, targets, prompt);
  2098.             except
  2099.               { The detection of this property failed. The most probably
  2100.                 reason for this is a bad desing of the component. For example
  2101.                 the TTable component raises an exaception if the MasterSource
  2102.                 property is read in the early state of the component. }
  2103.             end;
  2104.           end;
  2105.         end;
  2106.       end;
  2107.     finally
  2108.       list.Free;
  2109.     end;
  2110.   finally
  2111.     availableTargets.Free;
  2112.   end;
  2113. end;
  2114.  
  2115. procedure TIvTranslator.Loaded;
  2116. begin
  2117.   { Translates the host if auto translate is on and the dictionary is given. }
  2118.  
  2119.   if not IsDesignTime and
  2120.     (ivtoAutoOpen in FOptions) and
  2121.     ((FDictionary <> nil) or (Dictionaries.Count > 0)) then
  2122.   begin
  2123.     Translate;
  2124.   end;
  2125. end;
  2126.  
  2127. procedure TIvTranslator.Notification(component: TComponent; operation: TOperation);
  2128. begin
  2129.   inherited Notification(component, operation);
  2130.  
  2131.   if IsDesignTime and not (csReading in owner.ComponentState) and (operation = opInsert) then
  2132.   begin
  2133.     { Scans the component to add new targets }
  2134.  
  2135.     AddObjectTargets(component, FTargets, False);
  2136.   end;
  2137. end;
  2138.  
  2139. procedure TIvTranslator.ReadTargets(reader: TReader);
  2140. var
  2141.   i, version: Integer;
  2142.   target: TIvTargetProperty;
  2143. begin
  2144.   FTargets.ExcludeCount := 0;
  2145.   reader.ReadListBegin;
  2146.   version := reader.ReadInteger;
  2147.   if version > TARGETS_VERSION_C then
  2148.     raise EIvMulti.Create('Unknown target properties data version: ' + IntToStr(version));
  2149.  
  2150.   for i := 0 to reader.ReadInteger - 1 do
  2151.   begin
  2152.     { Reads the target from the stream }
  2153.  
  2154.     target := TIvTargetProperty.CreateDefault;
  2155.     reader.ReadListBegin;
  2156.     target.TargetClassName := reader.ReadString;
  2157.     target.TargetPropertyName := reader.ReadString;
  2158.     target.TargetType := TIvTargetType(reader.ReadInteger);
  2159.     reader.ReadListEnd;
  2160.  
  2161.     { Check if the target already exists. If not then adds the target. }
  2162.     
  2163.     if FTargets.Find(target) = -1 then
  2164.     begin
  2165.       if target.TargetType = ivttExclude then
  2166.         FTargets.ExcludeCount := FTargets.ExcludeCount + 1;
  2167.       FTargets.Add(target);
  2168.     end
  2169.     else
  2170.       target.Free;
  2171.   end;
  2172.   reader.ReadListEnd;
  2173. end;
  2174.  
  2175. procedure TIvTranslator.WriteTargets(writer: TWriter);
  2176. var
  2177.   i, j, count: Integer;
  2178.   exists: Boolean;
  2179.   targets: TIvTargetProperties;
  2180. begin
  2181. {$IFDEF WIN32}
  2182.   if (writer.Ancestor <> nil) and (writer.Ancestor is TIvTranslator) then
  2183.   begin
  2184.     targets := TIvTranslator(writer.Ancestor).Targets;
  2185.     count := 0;
  2186.     for i := 0 to FTargets.Count - 1 do
  2187.     begin
  2188.       { Checks of the parent form contains the target }
  2189.  
  2190.       exists := False;
  2191.       for j := 0 to targets.Count - 1 do
  2192.       begin
  2193.         if targets[j].IsEqual(FTargets[i]) then
  2194.         begin
  2195.           exists := True;
  2196.           Break;
  2197.         end;
  2198.       end;
  2199.  
  2200.       if not exists then
  2201.         Inc(count);
  2202.     end;
  2203.   end
  2204.   else
  2205. {$ENDIF}
  2206.   begin
  2207.     targets := nil;
  2208.     count := FTargets.Count;
  2209.   end;
  2210.  
  2211.   writer.WriteListBegin;
  2212.   writer.WriteInteger(TARGETS_VERSION_C);
  2213.   writer.WriteInteger(count);
  2214.   for i := 0 to FTargets.Count - 1 do
  2215.   begin
  2216.     { Checks of the parent form contains the target }
  2217.  
  2218.     exists := False;
  2219.     if targets <> nil then
  2220.     begin
  2221.       for j := 0 to targets.Count - 1 do
  2222.       begin
  2223.         if targets[j].IsEqual(FTargets[i]) then
  2224.         begin
  2225.           exists := True;
  2226.           Break;
  2227.         end;
  2228.       end;
  2229.     end;
  2230.  
  2231.     { Writes the target of the parent does not contain it. }
  2232.  
  2233.     if not exists then
  2234.     begin
  2235.       writer.WriteListBegin;
  2236.       with FTargets[i] do
  2237.       begin
  2238.         writer.WriteString(TargetClassName);
  2239.         writer.WriteString(TargetPropertyName);
  2240.         writer.WriteInteger(Integer(TargetType));
  2241.       end;
  2242.       writer.WriteListEnd;
  2243.     end;
  2244.   end;
  2245.   writer.WriteListEnd;
  2246. end;
  2247.  
  2248. procedure TIvTranslator.DefineProperties(filer: TFiler);
  2249.  
  2250.   function HasTargetsData: Boolean;
  2251.   begin
  2252.     if filer is TReader then
  2253.       Result := True
  2254.     else
  2255.     begin
  2256.       Result := FTargets.Count > 0;
  2257. {$IFDEF WIN32}
  2258.       if (filer.Ancestor <> nil) and (filer.Ancestor is TIvTranslator)then
  2259.         Result := Result and not FTargets.IsEqual(TIvTranslator(filer.Ancestor).Targets);
  2260. {$ENDIF}
  2261.     end;
  2262.   end;
  2263.  
  2264. begin
  2265.   inherited DefineProperties(filer);
  2266.  
  2267.   filer.DefineProperty('TargetsData', ReadTargets, WriteTargets, HasTargetsData);
  2268. end;
  2269.  
  2270. {$IFDEF WIN32}
  2271. { Return handle to a window containing the system menu }
  2272.  
  2273. function TIvTranslator.GetSystemMenuWinHandle: THandle;
  2274. begin
  2275.   { Return 0 if host does not have system menu }
  2276.  
  2277.   Result := 0;
  2278.   if Host is TWinControl then
  2279.   begin
  2280.     with TWinControl(Host) do
  2281.     begin
  2282.       if HandleAllocated then
  2283.         Result := Handle;
  2284.     end;
  2285.   end;
  2286. end;
  2287. {$ENDIF}
  2288.  
  2289.  
  2290. { Helper functions }
  2291.  
  2292. procedure IvCenterControl(parent, control: TControl);
  2293. begin
  2294.   if parent = nil then
  2295.   begin
  2296.     control.Left := (Screen.Width - control.Width) div 2;
  2297.     control.Top := (Screen.Height - control.Height) div 2;
  2298.   end
  2299.   else
  2300.   begin
  2301.     control.Left := parent.Left + (parent.Width - control.Width) div 2;
  2302.     control.Top := parent.Top + (parent.Height - control.Height) div 2;
  2303.   end;
  2304.  
  2305.   if control.Left < 0 then
  2306.     control.Left := 0;
  2307.  
  2308.   if control.Top < 0 then
  2309.     control.Top := 0;
  2310. end;
  2311.  
  2312. {$IFDEF WIN32}
  2313. initialization
  2314.   Modules := TIvModules.Create;
  2315. finalization
  2316.   Modules.Free;
  2317.   Modules := nil;
  2318. {$ELSE}
  2319. begin
  2320.   Modules := TIvModules.Create;
  2321. {$ENDIF}
  2322. end.
  2323.